package com.example.base.fragment;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.base.loadsir.EmptyCallback;
import com.example.base.loadsir.ErrorCallback;
import com.example.base.loadsir.LoadingCallback;
import com.example.base.utils.ToastUtil;
import com.example.base.viewmodel.MvvmBaseViewModel;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;
import com.example.base.R;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;

public abstract class MvvmFragment <V extends ViewDataBinding,VM extends MvvmBaseViewModel> extends Fragment implements IBasePagingView{
   protected VM viewModel;
   protected V viewDataBinding;
   protected String mFragmentTag = "";
   private LoadService mLoadService;

   public abstract int getBindingVariable();
   public abstract
   @LayoutRes
   int getLayoutId();
   public abstract VM getViewModel();

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      initParameters();
   }

   @Override
   public View onCreateView(@Nullable LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      viewDataBinding = DataBindingUtil.inflate(inflater, getLayoutId(), container, false);
      return viewDataBinding.getRoot();
   }

   @Override
   public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
      super.onViewCreated(view, savedInstanceState);
      viewModel = getViewModel();
      if (viewModel != null) {
         viewModel.attachUI(this);
      }
      if(getBindingVariable() > 0) {
         viewDataBinding.setVariable(getBindingVariable(), viewModel);
         viewDataBinding.executePendingBindings();
      }
   }
   /***
    *   初始化参数
    */
   protected void initParameters() {
   }

   @Override
   public void onDestroyView() {
      super.onDestroyView();
   }

   @Override
   public void onRefreshEmpty() {
      if(mLoadService != null){
         mLoadService.showCallback(EmptyCallback.class);
      }
   }

   @Override
   public void onRefreshFailure(String message) {
      if (mLoadService != null) {
         if(!isShowedContent) {
            mLoadService.showCallback(ErrorCallback.class);
         } else {
            ToastUtil.show(getContext(), message);
         }
      }
   }

   @Override
   public void showLoading() {
      if (mLoadService != null) {
         mLoadService.showCallback(LoadingCallback.class);
      }
   }
   private boolean isShowedContent = false;

   @Override
   public void showContent() {
      if (mLoadService != null) {
         isShowedContent = true;
         mLoadService.showSuccess();
      }
   }

   protected abstract void onRetryBtnClick();

   @Override
   public void onLoadMoreFailure(String message) {
      ToastUtil.show(getContext(), message);
   }

   @Override
   public void onLoadMoreEmpty() {
      ToastUtil.show(getContext(), getString(R.string.no_more_data));
   }

   @Override
   public void onActivityCreated(@Nullable Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
      Log.d(getFragmentTag(), this + ": " + "onActivityCreated");
   }

   @Override
   public void onAttach(Context context) {
      super.onAttach(getContext());
      Log.d(getFragmentTag(), this + ": " + "onAttach");
   }

   @Override
   public void onDetach() {
      super.onDetach();
      if (viewModel != null && viewModel.isUIAttached())
         viewModel.detachUI();
      Log.d(getFragmentTag(), this + ": " + "onDetach");
   }

   @Override
   public void onStop() {
      super.onStop();
      Log.d(getFragmentTag(), this + ": " + "onStop");
   }

   @Override
   public void onPause() {
      super.onPause();
      Log.d(getFragmentTag(), this + ": " + "onPause");
   }

   @Override
   public void onResume() {
      super.onResume();
      Log.d(getFragmentTag(), this + ": " + "onResume");
   }

   @Override
   public void onDestroy() {
      Log.d(getFragmentTag(), this + ": " + "onDestroy");
      super.onDestroy();
   }

   public void setLoadSir(View view) {
      // You can change the callback on sub thread directly.
      mLoadService = LoadSir.getDefault().register(view, new Callback.OnReloadListener() {
         @Override
         public void onReload(View v) {
            onRetryBtnClick();
         }
      });
   }

   protected abstract String getFragmentTag();
}
